package com.sha.api.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sha.api.web.domain.ApiResult;
import com.sha.api.web.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class ApiController {
    private final String close = "N";
    private final String prefix = "TY";
    private final String success = "{\"Status\":\"200\",\"Message\":\"【有效请求】查询成功\",\"OrderNumber\":\"TY1639224932749\",\"Result\":{\"VerifyResult\":1}}";

    @Value("${config.enable}")
    private String enable;

    @Value("${config.host}")
    private String host;

    @Value("${config.path}")
    private String path;

    @Value("${config.appKey}")
    private String appKey;

    @Value("${config.secretKey}")
    private String secretKey;

    @RequestMapping(value = {"/check-three-elements","/check-three-elements/{companyName}","/check-three-elements/{companyName}/{operName}","/check-three-elements/{companyName}/{operName}/{creditCode}","/check-three-elements/{companyName}/{operName}/{creditCode}/*"}, method = RequestMethod.GET)
    public ApiResult checkThreeElements(@PathVariable(required = false) String companyName, @PathVariable(required = false) String operName, @PathVariable(required = false) String creditCode){
        if(!StringUtils.hasLength(companyName) || !StringUtils.hasLength(operName) || !StringUtils.hasLength(creditCode)) {
            return ApiResult.warn("您的请求缺少必须的参数");
        }

        if(close.equals(enable)){
            JSONObject data = JSON.parseObject(success);
            return ApiResult.success("请求成功", data);
        }

        String timestamp = String.valueOf(new Date().getTime()/1000);
        String token = DigestUtils.md5DigestAsHex((appKey + timestamp + secretKey).getBytes());

        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Token", token.toUpperCase());
        headers.put("Timespan", timestamp);

        Map<String, String> querys = new HashMap<String, String>();
        querys.put("key", appKey);
        querys.put("companyName", companyName);
        querys.put("operName", operName);
        querys.put("creditCode", creditCode);

        try {
            HttpResponse response = HttpUtils.doGet(host, path, headers, querys);
            JSONObject data = JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
            String status = data.getString("Status");
            if(!"200".equals(status) && !"201".equals(status)){
                data.put("Status", "500");
                data.put("Message", "查询三要素异常");
            }
            data.put("OrderNumber", prefix + String.valueOf(new Date().getTime()));
            return ApiResult.success("请求成功", data);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResult.error("您的请求发生异常");
        }
    }
}
